package com.jzl.swing.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.eltima.components.ui.DatePicker;
import com.jzl.swing.entity.Bill;
import com.jzl.swing.entity.Supplier;
import com.jzl.swing.service.BillService;
import com.jzl.swing.service.SupplierService;
import com.jzl.swing.service.impl.BillServiceImpl;
import com.jzl.swing.service.impl.SupplierServiceImpl;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.awt.event.ActionEvent;

public class AddBillUI {

	private JFrame frame;
	private JTextField textFieldProductName;
	private JTextField textFieldProductNumber;
	private JTextField textFielduSpplierName;
	private JTextField textFieldPrice;
	private JLabel lblYyyy;
	BillService billService = new BillServiceImpl();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddBillUI window = new AddBillUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AddBillUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 800);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		datepick.setBounds(141, 680, 200, 50);
		frame.getContentPane().add(datepick);
		
		JLabel label = new JLabel("商品名称");
		label.setBounds(68, 41, 61, 16);
		frame.getContentPane().add(label);
		
		textFieldProductName = new JTextField();
		textFieldProductName.setBounds(141, 36, 130, 26);
		frame.getContentPane().add(textFieldProductName);
		textFieldProductName.setColumns(10);
		
		JLabel label_1 = new JLabel("交易数量");
		label_1.setBounds(68, 109, 61, 16);
		frame.getContentPane().add(label_1);
		
		textFieldProductNumber = new JTextField();
		textFieldProductNumber.setBounds(141, 104, 130, 26);
		frame.getContentPane().add(textFieldProductNumber);
		textFieldProductNumber.setColumns(10);
		
		JLabel label_2 = new JLabel("交易单位");
		label_2.setBounds(68, 185, 61, 16);
		frame.getContentPane().add(label_2);
		
		textFielduSpplierName = new JTextField();
		textFielduSpplierName.setBounds(141, 180, 130, 26);
		frame.getContentPane().add(textFielduSpplierName);
		textFielduSpplierName.setColumns(10);
		
		JLabel label_3 = new JLabel("交易金额");
		label_3.setBounds(68, 253, 61, 16);
		frame.getContentPane().add(label_3);
		
		textFieldPrice = new JTextField();
		textFieldPrice.setBounds(141, 248, 130, 26);
		frame.getContentPane().add(textFieldPrice);
		textFieldPrice.setColumns(10);
		
		JLabel label_4 = new JLabel("是否付款");
		label_4.setBounds(68, 309, 61, 16);
		frame.getContentPane().add(label_4);
		
		JComboBox<String> comboBoxIsPaid = new JComboBox<String>();
		comboBoxIsPaid.setModel(new DefaultComboBoxModel<String>(new String[] {"是", "否"}));
		comboBoxIsPaid.setBounds(141, 305, 77, 27);
		frame.getContentPane().add(comboBoxIsPaid);
		
		JLabel label_5 = new JLabel("所属供应商");
		label_5.setBounds(68, 369, 83, 16);
		frame.getContentPane().add(label_5);
		
		JComboBox<String> comboBoxSupplierName = new JComboBox<String>();
		SupplierService supplierService = new SupplierServiceImpl();
		List<Supplier> suppliers = new ArrayList<Supplier>();
		String[] suppliersStr;
		try {
			suppliers = supplierService.queryAll();
		} catch (Exception e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		suppliersStr = new String[suppliers.size()];
		for(int i = 0; i < suppliers.size(); i++) {
			suppliersStr[i] = suppliers.get(i).getSupplier_name();
		}
		comboBoxSupplierName.setModel(new DefaultComboBoxModel<String>(suppliersStr));
		comboBoxSupplierName.setBounds(145, 365, 125, 27);
		frame.getContentPane().add(comboBoxSupplierName);
		
		JLabel label_6 = new JLabel("商品描述");
		label_6.setBounds(68, 442, 61, 16);
		frame.getContentPane().add(label_6);
		
		JTextArea textAreaDescrib = new JTextArea();
		textAreaDescrib.setBounds(141, 442, 174, 85);
		frame.getContentPane().add(textAreaDescrib);
		
		JLabel label_7 = new JLabel("交易时间");
		label_7.setBounds(68, 627, 61, 16);
		frame.getContentPane().add(label_7);
		
		JButton button = new JButton("获取时间");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String simDate = simpleDateFormat.format(datepick.getValue());
                System.out.println(simDate);//这是一个java.util.Date对象
                lblYyyy.setText(simDate);
                JOptionPane.showMessageDialog(frame, "获取控件中的日期：" + lblYyyy.getText());
			}
		});
		button.setBounds(141, 659, 117, 29);
		frame.getContentPane().add(button);
		
		lblYyyy = new JLabel("yyyy-MM-dd");
		lblYyyy.setBounds(141, 627, 97, 16);
		frame.getContentPane().add(lblYyyy);
		
		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bill_product_name = textFieldProductName.getText();
				String bill_product_number = textFieldProductNumber.getText();
				String bill_price = textFieldPrice.getText();
				String bill_ispaid = comboBoxIsPaid.getSelectedItem().toString();
				String bill_supplier_name = comboBoxSupplierName.getSelectedItem().toString();
				String bill_product_describ = textAreaDescrib.getText();
				String bill_date = lblYyyy.getText();
				
				if ("".equals(bill_product_name)) {
					JOptionPane.showMessageDialog(frame, "商品名不能为空");
					return;
				}
				
				if ("".equals(bill_product_number)) {
					JOptionPane.showMessageDialog(frame, "交易数量不能为空");
					return;
				}
				try {
					Integer.parseInt(bill_product_number);
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(frame, "交易数量格式不正确");
					return;
				}
				
				if ("".equals(bill_price)) {
					JOptionPane.showMessageDialog(frame, "商品价格不能为空");
					return;
				}
				try {
					DecimalFormat df = new DecimalFormat("#.00");
					bill_price = String.valueOf(df.format(Double.parseDouble(bill_price)));
					
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(frame, "商品价格格式不正确");
					return;
				}
				
				
				
				
				
				Bill bill = new Bill();
				bill.setBill_product_name(bill_product_name);
				bill.setBill_product_number(bill_product_number);
				bill.setBill_price(bill_price);
				bill.setBill_ispaid(bill_ispaid);
				bill.setBill_supplierName(bill_supplier_name);
				bill.setBill_product_decrib(bill_product_describ);
				bill.setBill_date(bill_date);
				try {
					billService.insertBill(bill);
					JOptionPane.showMessageDialog(frame, "添加成功！");
					frame.dispose();
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(frame, "添加失败！请检查数据库连接与数据格式");
					e1.printStackTrace();
				}
			}
		});
		button_1.setBounds(34, 743, 117, 29);
		frame.getContentPane().add(button_1);
		
		JButton button_2 = new JButton("取消");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "确定退出吗？您填写的数据将不会被保存");
				if (i==0) {
					frame.dispose();
				}
			}
		});
		button_2.setBounds(243, 743, 117, 29);
		frame.getContentPane().add(button_2);
		
		JLabel label_8 = new JLabel("*");
		label_8.setForeground(Color.RED);
		label_8.setBounds(55, 41, 12, 16);
		frame.getContentPane().add(label_8);
		
		JLabel label_9 = new JLabel("*");
		label_9.setForeground(Color.RED);
		label_9.setBounds(55, 109, 12, 16);
		frame.getContentPane().add(label_9);
		
		JLabel label_10 = new JLabel("*");
		label_10.setForeground(Color.RED);
		label_10.setBounds(55, 185, 12, 16);
		frame.getContentPane().add(label_10);
		
		JLabel label_11 = new JLabel("*");
		label_11.setForeground(Color.RED);
		label_11.setBounds(55, 253, 12, 16);
		frame.getContentPane().add(label_11);
		
		JLabel label_12 = new JLabel("*");
		label_12.setForeground(Color.RED);
		label_12.setBounds(55, 309, 12, 16);
		frame.getContentPane().add(label_12);
		
		JLabel label_13 = new JLabel("*");
		label_13.setForeground(Color.RED);
		label_13.setBounds(55, 369, 12, 16);
		frame.getContentPane().add(label_13);
		
		JLabel label_14 = new JLabel("*");
		label_14.setForeground(Color.RED);
		label_14.setBounds(55, 627, 12, 16);
		frame.getContentPane().add(label_14);
		
	}
	
	final DatePicker datepick = getDatePicker();
	
    private DatePicker getDatePicker() {
        final DatePicker datepick;
        // 格式
        String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
        // 当前时间
        Date date = new Date();
        // 字体
        Font font = new Font("Times New Roman", Font.BOLD, 14);

        Dimension dimension = new Dimension(177, 24);

        int[] hilightDays = { 1, 3, 5, 7 };

        int[] disabledDays = { 4, 6, 5, 9 };
    //构造方法（初始时间，时间显示格式，字体，控件大小）
        datepick = new DatePicker(date, DefaultFormat, font, dimension);

        datepick.setLocation(137, 83);//设置起始位置
        /*
        //也可用setBounds()直接设置大小与位置
        datepick.setBounds(137, 83, 177, 24);
        */
        // 设置一个月份中需要高亮显示的日子
        datepick.setHightlightdays(hilightDays, Color.red);
        // 设置一个月份中不需要的日子，呈灰色显示
        datepick.setDisableddays(disabledDays);
        // 设置国家
        datepick.setLocale(Locale.CANADA);
        // 设置时钟面板可见
        datepick.setTimePanleVisible(true);
        return datepick;
    }
}
